package com.wangdao.wechatmall.controller.admin;

import com.wangdao.wechatmall.bean.bo.admin.OrderBO;
import com.wangdao.wechatmall.bean.bo.admin.RefundBO;
import com.wangdao.wechatmall.bean.bo.admin.ShipBO;
import com.wangdao.wechatmall.bean.vo.admin.BaseRespVo;
import com.wangdao.wechatmall.bean.vo.admin.OrderDetailVO;
import com.wangdao.wechatmall.bean.vo.admin.OrderVO;
import com.wangdao.wechatmall.exception.AdminIdException;
import com.wangdao.wechatmall.service.admin.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author liyongqiang
 * CreateDate 2021/7/10 9:23
 */

@RestController
@RequestMapping("admin/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @RequestMapping("list")
    public BaseRespVo queryList(@Valid OrderBO orderBO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            return BaseRespVo.fail("用户id输入格式错误");
        }
        OrderVO orderVO = orderService.queryList(orderBO);
        return BaseRespVo.ok(orderVO);
    }

    @RequestMapping("detail")
    public BaseRespVo showDetail(Integer id){
        OrderDetailVO orderDetailVO = orderService.showDetail(id);
        return BaseRespVo.ok(orderDetailVO);
    }

    @RequestMapping("ship")
    public BaseRespVo sendGoods(@RequestBody @Valid ShipBO shipBO, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            FieldError fieldError = bindingResult.getFieldError();
            assert fieldError != null;
            String defaultMessage = fieldError.getDefaultMessage();
            return BaseRespVo.fail(defaultMessage);
        }
        int send = orderService.sendGoods(shipBO);
        if (send == 1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody RefundBO refundBO){
        int refund = orderService.refundMoney(refundBO);
        if (refund == 1){
            return BaseRespVo.ok("退款成功");
        }
        return BaseRespVo.fail("退款失败");
    }

    /**
     * 订单评论回复，直接返回已回复
     * @return
     */
    @RequestMapping("reply")
    public BaseRespVo reply() {
        return BaseRespVo.fail("订单商品已回复！");
    }
}
